import request from '@/utils/request'
import type { ApiResult, ApiUser, BaseIds, PageSchema } from '@/api/api'
import type {
  AdminLoginDTO,
  AdminLoginVo,
  R2u,
  RefreshTokenDTO,
  User,
  UserAddDTO,
  UserModDTO,
  UserQueryDTO,
} from '@/api/acl/user/type'
import type { Role } from '@/api/acl/role/type.ts'

const BASE_URL = '/admin/api/v1/acl/user'
const API = {
  BASE_URL,
  LOGIN_URL: BASE_URL + '/anon/login',
  USER_ME_URL: BASE_URL + '/me',
  REFRESH_URL: BASE_URL + '/refresh',
  PAGE_URL: BASE_URL + '/page',
  LIST_URL: BASE_URL + '/list',
  REMOVE_URL: BASE_URL + '/rm',
  SWITCH_URL: BASE_URL + '/switch',
  GATHER_URL: BASE_URL + '/info/gather',
  R2U_URL: BASE_URL + '/r2u',
  USER_ROLES_URL: BASE_URL + '/roles',
}
//获取当前登录人信息
export const loginUser = () => {
  return request.get<any, ApiResult<ApiUser>>(API.USER_ME_URL)
}

//登录
export const login = (data: AdminLoginDTO) => {
  return request.post<any, ApiResult<AdminLoginVo>>(API.LOGIN_URL, data)
}

//刷新token
export const refreshLogin = (data: RefreshTokenDTO) => {
  return request.post(API.REFRESH_URL, data)
}
// 添加用户信息
export const userAdd = (data: UserAddDTO) => {
  return request.post<any, ApiResult<unknown>>(API.BASE_URL, data)
}
// 修改用户信息
export const userMod = (data: UserModDTO) => {
  return request.put<any, ApiResult<unknown>>(API.BASE_URL, data)
}
// 删除用户信息
export const userDel = (data: BaseIds) => {
  return request.post<any, ApiResult<unknown>>(API.REMOVE_URL, data)
}
// 查询用户信息
export const userList = (data: UserQueryDTO) => {
  return request.post<any, ApiResult<User[]>>(API.LIST_URL, data)
}
// 查询分页数据
export const userQuery = (data: UserQueryDTO) => {
  return request.post<any, ApiResult<PageSchema<User>>>(API.PAGE_URL, data)
}

export const switchUser = (id: string) => {
  return request.put<any, ApiResult<unknown>>(API.SWITCH_URL + `/${id}`)
}
export const gatherInfo = () => {
  return request.get<any, ApiResult<unknown>>(API.GATHER_URL)
}
export const r2u = (data: R2u) => {
  return request.post<any, ApiResult<unknown>>(API.R2U_URL, data)
}
export const userRoles = (id: string) => {
  return request.get<any, ApiResult<Role[]>>(API.USER_ROLES_URL + `/${id}`)
}
